﻿<controls:ChildWindow 
           xmlns:inputToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           xmlns:dataFormToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           x:Class="AdminModule.Views.EditWindow"
           xmlns:viewmodel="clr-namespace:AdminModule.ViewModels"
           xmlns:usercontrols="clr-namespace:AdminModule.UserControls.Common"
           xmlns:converter="clr-namespace:AdminModule.Converters"
           xmlns:validation="clr-namespace:InpValidator"
           xmlns:sys="clr-namespace:System;assembly=mscorlib"
           xmlns:userControlsDataInput="clr-namespace:AdminModule.UserControls.DataInput"
           Width="600" Height="440"
           Title="" 
           mc:Ignorable="d">
    
    <controls:ChildWindow.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Styles.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>

            <sys:String x:Key="nullComment">Сотрите содержимое, чтобы преобразовать в NULL</sys:String>
            <sys:String x:Key="comboBoxNullComment">Выберите пустую строку, чтобы преобразовать в NULL</sys:String>
        </ResourceDictionary>

        <viewmodel:EditWindowVM x:Key="viewModel"/>
        <converter:DateTimeConverter x:Key="dateTimeConverter" />

        <DataTemplate x:Key="AdmCategoriesByTerm">
            <StackPanel>
                <dataFormToolkit:DataField Label="Категория">
                    <ComboBox Style="{StaticResource editWindowComboBoxStyle}" x:Name="categoryComboBox" Loaded="categoryComboBox_Loaded" SelectionChanged="categoryComboBox_SelectionChanged" Tag="{Binding}" DisplayMemberPath="Name" ItemsSource="{Binding Path=foreignViewModel.Categories, Source={StaticResource viewModel}, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:ComboBoxNotNullValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </ComboBox>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Label="Вес">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Weight, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:CompositValidator Init="InitValidator">
                                <validation:CompositValidator.Childs>
                                    <validation:CheckDoubleValidator />
                                    <validation:ValueMoreThenZeroValidator />
                                </validation:CompositValidator.Childs>
                            </validation:CompositValidator>
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmGradation">
            <StackPanel>
                <dataFormToolkit:DataField Label="Градация">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Name, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:NotEmptyValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmDepartmentType">
            <StackPanel>
                <dataFormToolkit:DataField Label="Тип">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Name, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:NotEmptyValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmCategory">
            <StackPanel>
                <dataFormToolkit:DataField Label="Категория">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Name, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:NotEmptyValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Description="{StaticResource nullComment}" Label="Изображение">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding ImagePathForEdit, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmUnit">
            <StackPanel>
                <dataFormToolkit:DataField Label="Единица измерения">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Name, Mode=TwoWay}" x:Name="nameTextBox">
                        <validation:ValidatorService.Validator>
                            <validation:NotEmptyValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmDegree">
            <StackPanel>
                <dataFormToolkit:DataField Label="Название">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Name, Mode=TwoWay}" x:Name="nameTextBox">
                        <validation:ValidatorService.Validator>
                            <validation:NotEmptyValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmTerm">
            <StackPanel>
                <dataFormToolkit:DataField Label="Номер">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Order, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:ValueMoreThenZeroValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Label="Дата начала периода">
                    <controls:DatePicker IsTodayHighlighted="True" SelectedDate="{Binding StartDate, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Label="Дата окончания периода">
                    <controls:DatePicker IsTodayHighlighted="True" SelectedDate="{Binding StopDate, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Description="{StaticResource nullComment}" Label="Дата начала ввода">
                    <controls:DatePicker IsTodayHighlighted="True" SelectedDate="{Binding StartFill, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Description="{StaticResource nullComment}" Label="Дата окончания ввода">
                    <controls:DatePicker IsTodayHighlighted="True" SelectedDate="{Binding StopFill, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Description="{StaticResource nullComment}" Label="Фонд з/п">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Founds, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Description="{StaticResource nullComment}" Label="l1">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding l1, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Description="{StaticResource nullComment}" Label="l2">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding l2, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Description="{StaticResource nullComment}" Label="k1">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding k1, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Description="{StaticResource nullComment}" Label="k2">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding k2, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmPerson">
            <StackPanel>
                <dataFormToolkit:DataField Label="Фамилия">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding LastName, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:NotEmptyValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Label="Имя">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding FirstName, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:NotEmptyValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Label="Отчество">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Patronymic, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:NotEmptyValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Description="{StaticResource nullComment}" Label="Адрес электронной почты">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding EMailForEdit, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:RegexValidator Init="InitValidator" Expression="([0-9a-z_]+@[0-9a-z_^\.]+\.[a-z]{2,3})?"></validation:RegexValidator>
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmJob">
            <StackPanel>
                <dataFormToolkit:DataField Label="Должность">
                    <ComboBox Style="{StaticResource editWindowComboBoxStyle}" x:Name="positionComboBox" Loaded="positionComboBox_Loaded" SelectionChanged="positionComboBox_SelectionChanged" Tag="{Binding}" DisplayMemberPath="Name" ItemsSource="{Binding Path=foreignViewModel.AdmPositionCollection, Source={StaticResource viewModel}, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:ComboBoxNotNullValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </ComboBox>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Label="Ставка">
                    <ComboBox Style="{StaticResource editWindowComboBoxStyle}" x:Name="statusComboBox" Loaded="statusComboBox_Loaded" SelectionChanged="statusComboBox_SelectionChanged" Tag="{Binding}" DisplayMemberPath="Order" ItemsSource="{Binding Path=foreignViewModel.AdmStatusCollection, Source={StaticResource viewModel}, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:ComboBoxNotNullValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </ComboBox>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmPersonDegree">
            <StackPanel>
                <dataFormToolkit:DataField Label="Степень">
                    <ComboBox Style="{StaticResource editWindowComboBoxStyle}" x:Name="degreeComboBox" Loaded="degreeComboBox_Loaded" SelectionChanged="degreeComboBox_SelectionChanged" Tag="{Binding}" DisplayMemberPath="Name" ItemsSource="{Binding Path=foreignViewModel.AdmDegreeCollection, Source={StaticResource viewModel}, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:ComboBoxNotNullValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </ComboBox>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Label="Персона">
                    <ComboBox Style="{StaticResource editWindowComboBoxStyle}" x:Name="personForDegreeComboBox" Loaded="personForDegreeComboBox_Loaded" SelectionChanged="personForDegreeComboBox_SelectionChanged" Tag="{Binding}" ItemsSource="{Binding Path=foreignViewModel.AdmPersonsCollection, Source={StaticResource viewModel}, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:ComboBoxNotNullValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </ComboBox>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmStatus">
            <StackPanel>
                <dataFormToolkit:DataField Label="Ставка">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Order, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:CompositValidator Init="InitValidator">
                                <validation:CompositValidator.Childs>
                                    <validation:CheckDoubleValidator />
                                    <validation:ValueMoreThenZeroValidator />
                                </validation:CompositValidator.Childs>
                            </validation:CompositValidator>
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmPosition">
            <StackPanel>
                <dataFormToolkit:DataField Description="{StaticResource nullComment}" Label="Номер">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Order, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Label="Должность">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Name, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:NotEmptyValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Label="Общая по университету">
                    <CheckBox IsChecked="{Binding IsTotal, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Description="{StaticResource nullComment}" Label="SID">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding SIDForEdit, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmCharacteristic">
            <StackPanel>
                <dataFormToolkit:DataField Label="Показатель">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Name, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:NotEmptyValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmDepartment">
            <StackPanel>
                <dataFormToolkit:DataField Label="Тип">
                    <ComboBox Style="{StaticResource editWindowComboBoxStyle}" x:Name="typeDepartmentComboBox" Loaded="typeDepartmentComboBox_Loaded" SelectionChanged="typeDepartmentComboBox_SelectionChanged"  Tag="{Binding}" DisplayMemberPath="Name" ItemsSource="{Binding Path=foreignViewModel.depTypes, Source={StaticResource viewModel}, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:ComboBoxNotNullValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </ComboBox>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Label="Название">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Name, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:NotEmptyValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Label="Аббревиатура">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Abbriviation, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:NotEmptyValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Description="{StaticResource nullComment}" Label="SID">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding SIDForEdit, Mode=TwoWay}"/>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmSection">
            <StackPanel>
                <dataFormToolkit:DataField Label="Раздел">
                    <TextBox Style="{StaticResource editWindowTextBoxStyle}" Text="{Binding Name, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:NotEmptyValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </TextBox>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="AdmResponsible">
            <StackPanel>
                <dataFormToolkit:DataField Label="Должность">
                    <ComboBox Style="{StaticResource editWindowComboBoxStyle}" x:Name="positionComboBox" Loaded="positionComboBox_Loaded" SelectionChanged="positionComboBox_SelectionChanged" Tag="{Binding}" DisplayMemberPath="Name" ItemsSource="{Binding Path=foreignViewModel.AdmPositionCollection, Source={StaticResource viewModel}, Mode=TwoWay}">
                        <validation:ValidatorService.Validator>
                            <validation:ComboBoxNotNullValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </ComboBox>
                </dataFormToolkit:DataField>
                <dataFormToolkit:DataField Description="{StaticResource comboBoxNullComment}" Label="Отдел">
                    <ComboBox Style="{StaticResource editWindowComboBoxStyle}" x:Name="departmentComboBox" Loaded="departmentComboBox_Loaded" SelectionChanged="departmentComboBox_SelectionChanged" Tag="{Binding}" ItemsSource="{Binding Path=foreignViewModel.AdmDepartmentCollection, Source={StaticResource viewModel}, Mode=TwoWay}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock MinHeight="17" Text="{Binding FullName}"></TextBlock>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                        <validation:ValidatorService.Validator>
                            <validation:ComboBoxNotNullValidator Init="InitValidator" />
                        </validation:ValidatorService.Validator>
                    </ComboBox>
                </dataFormToolkit:DataField>
            </StackPanel>
        </DataTemplate>

    </controls:ChildWindow.Resources>
    
    <Grid x:Name="LayoutRoot" Margin="2">

        <validation:ValidatorManager x:Name="MainManager"/>

        <dataFormToolkit:DataForm
            EditEnded="editDataForm_EditEnded"
            EditEnding="editDataForm_EditEnding"
            CommandButtonsVisibility="Commit"
            CommitButtonContent="Сохранить"
            AutoEdit="True"
            AutoCommit="False"
            AutoGenerateFields="False"
            x:Name="editDataForm"
            CurrentItem="{Binding Path=objectForEdit, Source={StaticResource viewModel}, Mode=TwoWay}" Margin="0,0,0,30">
        </dataFormToolkit:DataForm>
        
        <Button Click="cancelButton_Click" x:Name="cancelButton" HorizontalAlignment="Right" Margin="0,0,8,4" VerticalAlignment="Bottom" Width="75" Content="Отмена" />
        <usercontrols:ProgressBar IsVisibility="Collapsed" VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="progressBar"></usercontrols:ProgressBar>
    </Grid>
</controls:ChildWindow>